import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import( '../views/login.vue'), //登入页面
    },

    {
        path: '/home',
        component: () => import( '../views/home.vue'), //后台主体框架
        redirect: '/system/welcome',
        children: [
            {
                path: '/system/welcome',
                name: 'Welcome',
                component: () => import( '../views/welcome.vue'), //系统欢迎页
            }, {
                path: '/system/users',
                name: 'Users',
                component: () => import( '../views/system/user.vue'), //用户管理
            }, {
                path: '/system/roles',
                name: '系统管理 > 角色管理',
                component: () => import( '../views/system/role.vue'), //角色管理
            }
            , {
                path: '/system/menu-category',
                name: 'Menu-category',
                component: () => import( '../views/system/menu-category.vue'), //菜单管理
            }
            , {
                path: '/system/departments',
                name: 'Departments',
                component: () => import( '../views/system/dept.vue'), // 部门管理
            },
            {
                path: '/system/emp',
                name: 'Emp',
                component: () => import('../views/system/emp'),  //员工管理
            },

            {
                path: '/system/icon',
                name: 'Icons',
                component: () => import('../views/system/icon'),  //系统图标
            }
            /**********************************业务模块的路由******************************/
            , {
                path: '/student/class',
                name: '班级信息管理 / 班级管理',
                component: () => import( '../views/business/stu/ClassManage'), //班级管理
            }
            , {
                path: '/student/enter-exam',
                name: '班级信息管理/报考管理',
                component: () => import( '../views/business/stu/Adultexam'), //学员缴费管理
            },
            , {
                path: '/student/getjob',
                name: '班级信息管理/就业管理',
                component: () => import( '../views/business/stu/ObtainEmp'), //学员缴费管理
            },
            , {
                path: '/pay/manage',
                name: '财务信息管理/学员缴费管理',
                component: () => import( '../views/business/pay/PayMent'), //学员缴费管理
            },
            , {
                path: '/payType/manage',
                name: '财务信息管理/缴费项目管理',
                component: () => import( '../views/business/pay/PayTypeMent'), //学员缴费管理
            },
            {
                path: '/student/stay',
                name: '财务信息管理/住宿管理',
                component: () => import( '../views/business/stu/StuStay'), //住宿
            },
            {
                path: "/error/401",
                component: () => import( '../views/error/401'),
            },

            /***************************************咨询模块的路由*********************************/
            {
                path: '/consult/manage',
                name: '学生信息管理/咨询管理',
                component: () => import( '../views/consult/consultant'), //咨询管理
            },
            /***************************************学生模块的路由*********************************/
            {
                path: '/student/manage',
                name: '学生信息管理/学生管理',
                component: () => import( '../views/business/stu/StudentManage'), //学生管理
            },
            /***************************************年级模块的路由*********************************/
            {
                path: '/student/grade',
                name: '学生信息管理/年级管理',
                component: () => import( '../views/business/stu/Grade'), //年级管理
            },
            /***************************************考试项目模块的路由*********************************/
            {
                path: '/student/project',
                name: '学生信息管理/考试项目管理',
                component: () => import( '../views/business/stu/ExamType'), //考试项目管理
            },
            /***************************************学员成绩模块的路由*********************************/
            {
                path: '/student/mark',
                name: '学生信息管理/学员成绩管理',
                component: () => import( '../views/business/stu/StudentScore'), //学员成绩管理
            }
        ]
    },
]

const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
})

import store from '../store'//引入store

//白名单页面
const whiteList=[
    '/business/product/add-stocks',
    '/business/product/publish'
];

//路由导航守卫
router.beforeEach((to, from, next) => {

    const token = LocalStorage.get(LOCAL_KEY_XINGUAN_ACCESS_TOKEN);
    if (to.path === '/login') {
        if (!token) {
            return next();
        } else {
            return next({path: '/home'})
        }
    }

    if (to.path === '/error/401') {
        return next();
    }

    if (!token) {
        return next('/login');
    } else {
        //判断是否有访问该路径的权限
        const urls = store.state.userInfo.url;
        //如果是管理员
        if (store.state.userInfo.isAdmin) {
            return next();
        } else {
            if (urls.indexOf(to.path) > -1|| whiteList.indexOf(to.path)>-1) {
                //则包含该元素
                window.sessionStorage.setItem("activePath", to.path);
                return next();
            } else {
                return next("/error/401");
            }
        }
    }
})

export default router
